<!DOCTYPE html>
<html>
<body>
<h2>LOGIN</h2>
<button type="button" onclick="onLoginClicked()">Login</button><br>
<button type="button" onclick="onAddAnouceClicked()">Add Anouce</button><br>
<button type="button" onclick="onDelAnouceClicked()">Del Anouce</button><br>
<textarea id="demo" rows="5" cols="100"></textarea>
<script>

window.token = "";

function post(options){
    var data = options.data;
    var callback = options.onSucceed;
    var http = new XMLHttpRequest();
    http.onload = function(){
        var json = JSON.parse(this.responseText);
        callback && callback(json);
    }
    http.open("POST", options.url, true)
    http.send(JSON.stringify(data));
}

function onLoginClicked(){
  post({
      data:{account:"admin", password:"123qwe"},
      url:"http://wx.taoke93.com:8100/api/auth/login",
      onSucceed:function(d){
          if(d.stat == "ok"){
              window.token = d.session.token;
          }  
          document.getElementById("demo").value = JSON.stringify(d);        
      }
  });
}

function onAddAnouceClicked(){
    //add(string token, string title, long image, string source, string content)
  post({
      data:{token:window.token, title:"from hs test", image:0, source:"from hs", content:"huang zhong shi fan"},
      url:"http://wx.taoke93.com:8100/api/anouce/add",
      onSucceed:function(d){
          if(d.stat == "ok"){
            document.getElementById("demo").value = JSON.stringify(d.ret);
            window.aid = d.ret.id;
          }
          else{
              document.getElementById("demo").value = JSON.stringify(d);
          }
      }
  });
    
}

function onDelAnouceClicked(){
    //add(string token, string title, long image, string source, string content)
  post({
      data:{token:window.token, aid:window.aid},
      url:"http://wx.taoke93.com:8100/api/anouce/delete",
      onSucceed:function(d){
         document.getElementById("demo").value = JSON.stringify(d.stat);
      }
  });
    
}

</script>

</body>
</html>
